package com.taihua_tech.basic.util;

 

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

 
/**
 * 数据参数类
 */
public class DataParam extends DataMap implements Serializable{
	private static final long serialVersionUID = -4694888249797010615L;
	
	public static final String STATE_PREFIX = "state_";
	public static final String INSERT_TAG = "insert";
	public static final String UPDATE_TAG = "update";
	
	List<DataParam> insertParam = new ArrayList<DataParam>();
	List<DataParam> updateParam = new ArrayList<DataParam>();
	public DataParam() {
		super();
	}
	public DataParam(Object... args) {
		super(args);
	}	
	protected boolean isValid(String value){
		return !(value == null || value.equals("null") || value.trim().equals(""));
	}
	@SuppressWarnings("rawtypes")
	public DataParam(Map map){
		Iterator iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			String[] temp = (String[])map.get(key);
			if (temp != null){
				int length = temp.length;
				if (length == 1){
					String value = temp[0];
					this.put(key, value);					
				}else{
					for (int i=0;i < length;i++){
						String values = StringUtil.append(temp, ",");
						this.put(key,values);
					}
				}
			}
		}
	}
	public Object getObject(String key){
		return super.get(key);
	}
    public Object get(Object key) {
    	if (key instanceof String){
    		String sKey = (String)key;
    		if (sKey.endsWith("_like")){
    			Object value = (Object)super.get(sKey.substring(0,sKey.length()-5));
        		if (value != null && !"".equals(value) && value instanceof String){
        			return "%"+value+"%";        			
        		}
    		}
    		else if (sKey.endsWith("_llike")){
    			Object value = (Object)super.get(sKey.substring(0,sKey.length()-6));
        		if (value != null && !"".equals(value) && value instanceof String){
        			return "%"+value;        			
        		}
    		}
    		else if (sKey.endsWith("_rlike")){
    			Object value = (Object)super.get(sKey.substring(0,sKey.length()-6));
    			if (value != null && !"".equals(value) && value instanceof String){
    				return value+"%";    				
    			}
    		}    			
    	}
    	return super.get(key);
    }	
	public String get(String key){
		String value = (String)super.get(key);
		return value;	
	}
	public String get(String key,String defaultValue){
		String value = (String)super.get(key);
		if (StringUtil.isNullOrEmpty(value)){
			value = defaultValue;
		}
		return value;	
	}
	
	public DataParam merge(DataParam param){
		Iterator<String> iter = param.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			Object value = param.getObject(key);
			if (value != null){
				this.put(key, value);				
			}
		}
		return this;
	}
	
	public Iterator<String> getNames(){
		return this.keySet().iterator();
	}
	public void buildParam(String statePrefix){
		Iterator<String> iter = this.getNames();
		while(iter.hasNext()){
			String param = iter.next();
			if (param.startsWith(statePrefix)){
				String rowId = param.substring(statePrefix.length(),param.length());
				if (!isValidRowId(rowId))continue;
				String value = this.get(param);
				if (INSERT_TAG.equals(value)){
					DataParam dataParam = getParam(rowId); 
					this.insertParam.add(dataParam);	
				}
				else if (UPDATE_TAG.equals(value)){
					DataParam dataParam = getParam(rowId); 
					this.updateParam.add(dataParam);
				}
			}
		}
	}
	private boolean isValidRowId(String rowId){
		boolean result = true;
		try {
			Integer.parseInt(rowId);	
		} catch (Exception e) {
			result = false;
		}
		return result;
	}
	public List<DataParam> getInsertParam() {
		return insertParam;
	}
	public List<DataParam> getUpdateParam() {
		return updateParam;
	}
	private DataParam getParam(String rowId){
		DataParam result = new DataParam();
		Iterator<String> iter = this.getNames();
		while(iter.hasNext()){
			String key = iter.next();
			if (key.endsWith("_"+rowId)){
				String newKey = key.substring(0,key.lastIndexOf(rowId)-1);
				result.put(newKey,String.valueOf(super.get(key))); 
			}
		}
		return result;
	}
}
